import { IProxy } from '@/typing';
import { catchError, defer, forkJoin, map, Observable, of, Subject } from 'rxjs';
import BaseSourceFetcher from '@/modules/proxy/proxy-source/base-source-fetcher';
import IHelper from '@/services/helper.service';
import axios from 'axios';
import * as cheerio from 'cheerio';

export default class ProxySource02 extends BaseSourceFetcher {
  updater$: Subject<IProxy>
  fetch(updater$: Subject<IProxy>) {
    this.updater$ = updater$
    forkJoin(
      [...IHelper.getArray(100).map(page => this.getPerPage(page + 1))]
    ).subscribe()
  }

  getPerPage(page: number) {
    return defer(() =>
      axios.get(`https://www.89ip.cn/index_${page}.html`).then(d => d.data)
    ).pipe(
      map(html => this.parseHtml(html)),
      catchError(() => of([] as IProxy[]))
    )
  }
  parseHtml(html: string) {
    const $ = cheerio.load(html)
    const trList = $("table tbody tr")
    const proxyList: IProxy[] = IHelper.getArray(trList.length).map(idx => this.parseRow(trList.eq(idx)))
    return proxyList
  }

  parseRow(row: cheerio.Cheerio<cheerio.Element>): IProxy {
    const proxy: IProxy = {
      host: row.find("td").eq(0).text().trim(),
      port: +row.find("td").eq(1).text().trim(),
      last_time: IHelper.timeFormatted(),
      region: "",
      source: "source-02"
    }
    this.updater$.next(proxy)
    return proxy
  }
}